import requests
from requests_toolbelt.multipart import encoder
from pathlib import Path
import time

header = {"authorization": "xxxx"}  # 请求的header，如有则加上
satrt_time = time.time()


def progress_callback(monitor):
    print(type(monitor))
    # 进度的callback函数
    progress = (monitor.bytes_read / monitor.len) * 100
    seduup = monitor.bytes_read / 1024 / 1024 / (time.time() - satrt_time)
    print("\r 网速：%d MB/s 文件上传进度：%d%%(%d/%d)" % (seduup, progress, monitor.bytes_read, monitor.len), end=" ")


file_name = Path('./dist/百度识图.exe')

e = encoder.MultipartEncoder(
    fields={'file': (file_name.name, file_name.open('rb'), 'application/x-zip-compressed'),
            "key1": "value1", "key2": "value2"}  # post的参数key1\key2直接写在这里
)
m = encoder.MultipartEncoderMonitor(e, progress_callback)
header["Content-Type"] = m.content_type  # header增加m.content_type，必填
r = requests.post(url='http://localhost:8000/API/AICV/', data=m, headers=header)
print("\n 请求结果：", r.json())
